package me.zhengjie.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.Calendar;
import java.util.Date;

public class CalculateAge {

    /**
     * 根据生日计算当前周岁数
     */
    public static int getCurrentAge(String sfz) {
        // 当前时间
        Calendar curr = Calendar.getInstance();
        // 生日
        Calendar born = Calendar.getInstance();
        Date birthday = formetBirthday(sfz);
        born.setTime(birthday);
        // 年龄 = 当前年 - 出生年
        int age = curr.get(Calendar.YEAR) - born.get(Calendar.YEAR);
        if (age <= 0) {
            return 0;
        }
        // 如果当前月份小于出生月份: age-1
        // 如果当前月份等于出生月份, 且当前日小于出生日: age-1
        int currMonth = curr.get(Calendar.MONTH);
        int currDay = curr.get(Calendar.DAY_OF_MONTH);
        int bornMonth = born.get(Calendar.MONTH);
        int bornDay = born.get(Calendar.DAY_OF_MONTH);
        if ((currMonth < bornMonth) || (currMonth == bornMonth && currDay <= bornDay)) {
            age--;
        }
        return age < 0 ? 0 : age;
    }


    public static String getSndj(String adlFs, int age) {

        int fs = Integer.parseInt(adlFs);

        JSONObject obj  = new JSONObject();
        if (age >= 90) {

            if(fs <= 20) {
                obj.put("sndj", "重度失能");
                obj.put("dyqx", "24");
            } else if(fs > 20 && fs <= 30) {
                obj.put("sndj", "重度失能");
                obj.put("dyqx", "12");
            } else if(fs > 40 && fs <= 100) {
                obj.put("sndj", "重度失能");
                obj.put("bxbl", "按照现行报销比例的70%补偿");
            }
        }
        return null;
    }


    private static Date formetBirthday(String sfz) {
        String date = sfz.substring(6, 14);
        date = date.substring(0, 4)+"-"+date.substring(4, 6)+"-"+date.substring(6, 8);
        Date birthDate = DateTimeUtil.parse(date, DateTimeUtil.DEFAULT_DATE_FORMAT);

        System.out.println(birthDate);
        return birthDate;
    }

}
